package org.digitaldomination.thefinalcountdown;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	
	public final static String KEY_ROWID = "_id";
	public final static String KEY_TITLE = "title";
	public final static String KEY_DATE_TO = "date_to";
	public final static String KEY_DATE_FROM = "date_from";
	public final static String KEY_COUNT_TYPE = "count_type";


	private final static String TAG = "DBAdapter";
	
	private final static String DATABASE_NAME = "thefinalcountdown";
	private final static String DATABASE_TABLE = "counters";
	private final static int DATABASE_VERSION = 16;
	
	private final static String DATABASE_CREATE = 
			"create table counters (_id integer primary key autoincrement, " 
			+ "title text not null, date_to integer not null, date_from integer not null, count_type integer not null)";
	
	private final Context context;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DBAdapter(Context ctx)
	{
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{
		DatabaseHelper(Context context)
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try{
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e){
				e.printStackTrace();
			}
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
		{
			Log.w(TAG, "Upgrading databases from version " + oldVersion + "to " 
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS counters");
			
			onCreate(db);
		}
	}
	
	//--- Open the database ---
	public DBAdapter open() throws SQLException
	{
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//--- Close the database ---
	public void close()
	{
		DBHelper.close();
	}
	
	//--- Insert into the database ---
	public long queryaddcountdown(long date_to, long date_from, long count_type, String title)
	{
		ContentValues initialValue = new ContentValues();
		initialValue.put(KEY_TITLE, title);
		initialValue.put(KEY_DATE_TO, date_to);
		initialValue.put(KEY_DATE_FROM, date_from);
		initialValue.put(KEY_COUNT_TYPE, count_type);
		
		return db.insert(DATABASE_TABLE, null, initialValue);
	}
	
	//--- Delete a particular ---
	public boolean deletecountdown(long id)
	{ 
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + id, null) > 0;
	}
	
	//--- Retrieves all ---
	public Cursor querygetcountdowns(int count_type)
	{
		return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
				KEY_DATE_TO, KEY_DATE_FROM, KEY_COUNT_TYPE},  KEY_COUNT_TYPE + "=" + count_type, null, null, null, null);
	}
	
	//--- Retrieves a particular ---
	public Cursor getsinglecount(long rowId) throws SQLException
	{
		Cursor mCursor =
				db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, 
						KEY_DATE_TO, KEY_DATE_FROM, KEY_COUNT_TYPE}, KEY_ROWID + "=" + rowId, null, 
						null, null, null, null);
		if(mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	//--- Updates ---
	public boolean updateContact(long rowId, String name, String email)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_TITLE, name);
		args.put(KEY_DATE_TO, email);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
	}	
}